//往集合中添加单个文档

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
const dbName = 'runoob';
//连接到MongoDB
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
    if (err) throw err;
    const db = client.db(dbName); //选择数据库
    const coll = db.collection('bookinfo'); // 选择一个集合(表)
    //执行添加文档操作

    // 添加一个文档
    const myobj = {
        "isbn": "19301041",
        "name": "javaE 企业级开发",
        "author": "向向",
        "press ": "清华大学出版社 ",

        "price ": 79.00,
        "pubdate ": "2018 - 02 - 01 "
    };



    coll.insertOne(myobj, (err, res) => {
        if (err) throw err;
        console.log("文档添加成功");
        console.log("输出结果", res); //输出结果
        console.log("选择数据库", db);
        console.log("选择一个集合(表)", coll);
        client.close(); //关闭连接
    });

});